package com.android.namecard;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

import com.android.namecard.entity.Namecard;
import com.android.namecard.model.NamecardDBHelper;

public class DBTestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.db_test);
        Button button1 = (Button)findViewById(R.id.button1);
        Button button2 = (Button)findViewById(R.id.button2);
        Button button3 = (Button)findViewById(R.id.button3);
        OnClickListener listener1 = new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String name = "王昊";
				String career = "学生";
				String company = "华南理工大学";
				String address = "华工生活区c14-209";
				String email = "wanghao@gmail.com";
				String phone = "13678099475";
				String qq = "416238943";
				String msn = "lyy@gmail.com";
				String website = "http://wanghao.scut.com";
				String business = "派传单，送外卖";
				String type = "private";
				Resources res = getResources();
				Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.icon);
				NamecardDBHelper namecardDBHelper = new NamecardDBHelper(v.getContext());
				boolean b = namecardDBHelper.insertNamecard(name, career, company, address, email, phone, qq, msn, website, business, type, bmp, bmp);
			    setTitle(String.valueOf(b));
			}
        	
        };
        
        OnClickListener listener2 = new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				NamecardDBHelper namecardDBHelper = new NamecardDBHelper(v.getContext());
				Namecard namecard = namecardDBHelper.getNamecard();
				if(namecard!=null){
					setTitle(namecard.getAddress());
					ImageView imageView = (ImageView)findViewById(R.id.image1);
					imageView.setImageBitmap(namecard.getFrontImage());
				}
			}
        	
        };
        
        OnClickListener listener3 = new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				NamecardDBHelper namecardDBHelper = new NamecardDBHelper(v.getContext());
				boolean b = namecardDBHelper.deleteAllNamecards();
				//deleteDatabase(NamecardDBHelper.DATABASE_NAME);
				Log.i("The delete result is", String.valueOf(b));
			}
        	
        };
        
        button1.setOnClickListener(listener1);
        button2.setOnClickListener(listener2);
        button3.setOnClickListener(listener3);
    }
}